package com.gxa.community.admin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.service.system.OperationLogService;
import com.gxa.community.core.pojo.domain.OperationLog;
import com.gxa.community.core.pojo.dto.system.OperationLogsDTO;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SuppressWarnings("ALL")
@Slf4j
@RestController
@RequestMapping("/operationlog")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    @SuppressWarnings("DuplicatedCode")
    @ApiOperation("查询所有操作日志")
    @GetMapping("/getalloperationlog")
    public R getAllOperationLog(@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        //设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        List<OperationLog> list = operationLogService.list();
        if (!CollectionUtils.isEmpty(list)){
            PageInfo<OperationLog> pageInfo = new PageInfo<>(list);
            OperationLogsDTO operationLogsDTO = new OperationLogsDTO();
            operationLogsDTO.setTotal(pageInfo.getTotal());
            operationLogsDTO.setPages(pageInfo.getPages());
            operationLogsDTO.setCurrentPage(currentPage);
            operationLogsDTO.setPageSize(pageSize);
            operationLogsDTO.setOperationLogs(pageInfo.getList());
            return R.ok().put("data",operationLogsDTO);
        }
        return R.error(600,"查询失败！请联系管理员！");
    }


    @ApiOperation("模糊搜索操作日志记录")
    @GetMapping("/searchoperationlog")
    public R searchOperationLog(@RequestParam(value = "loginAct",required = false) String loginAct,@RequestParam(value = "operationTimeStart",required = false) Long operationTimeStart,@RequestParam(value = "operationTimeEnd",required = false) Long operationTimeEnd,@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        if (StringUtils.isEmpty(loginAct) && operationTimeStart==null && operationTimeEnd==null){
            List<OperationLog> list = operationLogService.list();
            if (!CollectionUtils.isEmpty(list)){
                PageInfo<OperationLog> pageInfo = new PageInfo<>(list);
                OperationLogsDTO operationLogsDTO = new OperationLogsDTO();
                operationLogsDTO.setTotal(pageInfo.getTotal());
                operationLogsDTO.setPages(pageInfo.getPages());
                operationLogsDTO.setCurrentPage(currentPage);
                operationLogsDTO.setPageSize(pageSize);
                operationLogsDTO.setOperationLogs(pageInfo.getList());
                return R.ok().put("data",operationLogsDTO);
            }else {
                return R.error(600,"查询失败！请联系管理员！");
            }
        }else {
            LambdaQueryWrapper<OperationLog> operationLogLambdaQueryWrapper = Wrappers.lambdaQuery(OperationLog.class);
            operationLogLambdaQueryWrapper.like(!StringUtils.isEmpty(loginAct),OperationLog::getLoginAct,loginAct);
            operationLogLambdaQueryWrapper.gt(operationTimeStart!=null,OperationLog::getOperationTime,operationTimeStart);
            operationLogLambdaQueryWrapper.lt(operationTimeEnd!=null,OperationLog::getOperationTime,operationTimeEnd);
            List<OperationLog> list = operationLogService.list(operationLogLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(list)){
                PageInfo<OperationLog> pageInfo = new PageInfo<>(list);
                OperationLogsDTO operationLogsDTO = new OperationLogsDTO();
                operationLogsDTO.setTotal(pageInfo.getTotal());
                operationLogsDTO.setPages(pageInfo.getPages());
                operationLogsDTO.setCurrentPage(currentPage);
                operationLogsDTO.setPageSize(pageSize);
                operationLogsDTO.setOperationLogs(pageInfo.getList());
                return R.ok().put("data",operationLogsDTO);
            }else {
                return R.error(600,"查询失败！请联系管理员！");
            }
        }
    }
}
